/**
 * 
 */
package com.lingyun.ucm.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author Terrence2013
 * 2018年1月24日 上午9:49:22
 * TODO
 */
public final class TimeUtil {
	
	private TimeUtil(){};
	
	/******yyyyMMddHHmmssSSS***/
	public static final String FORMAT_MILSECOND = "yyyyMMddHHmmssSSS";
	/**yyyy-MM-dd HH:mm:ss**/
	public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
	/**yyyy-MM-dd HH:mm**/
	public static final String FORMAT_MIN = "yyyy-MM-dd HH:mm";
	/**yyyy-MM-dd**/
	public static final String FORMAT_DAY = "yyyy-MM-dd";
	/**HH:mm:ss**/
	public static final String FORMAT_TIME = "HH:mm:ss";
	/**
	 * 
	 * 将日期转成特定格式的字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToStr(Date date, String format) {
		if(null == date || format.isEmpty()){
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	/**
	 * 获取当前日期时间
	 * @return
	 */
	public static Date getCurrentDate(){
		return Calendar.getInstance().getTime();
	}
	
	/**
	 * 获取当前时间两小时后的时间
	 * @return
	 */
	public static Date getDateAfterTwoHour(){
		Date now = TimeUtil.getCurrentDate();
		Date afterTwoHour = new Date(now.getTime()+1000*60*60*2);
		return afterTwoHour;
	}

	/**
	 * 
	 * 将日期字符串转成Date
	 * @param dateStr
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date strToDate(String dateStr, String format) throws ParseException {
		if(dateStr.isEmpty() || format.isEmpty()){
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(dateStr);
	}
	
	public static void main(String[] args){
		Date now = Calendar.getInstance().getTime();
		Date afterTwoHour = new Date(now.getTime()+1000*60*60*20);
		System.out.println(TimeUtil.dateToStr(now, FORMAT_SECOND));
		System.out.println(TimeUtil.dateToStr(afterTwoHour, FORMAT_SECOND));
	}
}
